"""UI组件模块"""
import tkinter as tk
from tkinter import ttk
import os
from PIL import Image, ImageTk


class UIManager:
    """UI管理器"""
    
    WINDOW_WIDTH = 800
    WINDOW_HEIGHT = 640
    TITLE_BAR_HEIGHT = 50
    LOGO_SIZE = (800, 50)
    VERSION = "Version 1.0.1"
    
    def __init__(self, root):
        self.root = root
        self.logo_image = None
    
    def setup_window(self):
        self.root.title("lightOS 自动化配置工具")
        self.root.geometry(f"{self.WINDOW_WIDTH}x{self.WINDOW_HEIGHT}")
        self.root.minsize(self.WINDOW_WIDTH, self.WINDOW_HEIGHT)
        self.root.resizable(False, False)
    
    def setup_styles(self):
        style = ttk.Style()
        style.configure("Config.TLabelframe.Label", font=("宋体", 20, "bold"))
        style.configure("TNotebook.Tab", padding=[20, 10], font=("宋体", 9))
        style.map("TNotebook.Tab",
                  font=[("selected", ("宋体", 13, "bold"))],
                  foreground=[("selected", "#0026FF")])
    
    def create_title_bar(self):
        title_bar = tk.Frame(self.root, height=self.TITLE_BAR_HEIGHT)
        title_bar.pack(fill=tk.BOTH, side=tk.TOP, expand=False)
        title_bar.pack_propagate(False)
        
        try:
            logo_path = os.path.join(os.path.dirname(__file__), "logo.png")
            if os.path.exists(logo_path):
                img = Image.open(logo_path).resize(self.LOGO_SIZE, Image.Resampling.LANCZOS)
                self.logo_image = ImageTk.PhotoImage(img)
                logo = tk.Label(title_bar, image=self.logo_image, bg="white", bd=0)
                logo.pack(fill=tk.BOTH, expand=True)
        except Exception as e:
            print(f"加载logo失败: {e}")
        
        return title_bar
    
    def create_main_frame(self):
        main_frame = ttk.Frame(self.root, padding="0")
        main_frame.pack(fill=tk.BOTH, expand=True)
        return main_frame
    
    def create_notebook(self, parent):
        notebook = ttk.Notebook(parent)
        notebook.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        return notebook
    
    def create_sheet_tab(self, notebook, sheet_name):
        sheet_frame = ttk.Frame(notebook)
        notebook.add(sheet_frame, text=sheet_name)
        return sheet_frame
    
    def create_title_section(self, parent):
        title_frame = tk.Frame(parent)
        title_frame.pack(fill=tk.X, padx=5, pady=5)
        
        title_content_frame = tk.Frame(title_frame)
        title_content_frame.pack(fill=tk.X)
        
        title_label = tk.Label(title_content_frame, text="", font=("宋体", 12, "bold"))
        title_label.pack(side=tk.LEFT, padx=0, pady=5)
        
        separator = tk.Frame(title_content_frame, height=1, bg="gray", relief=tk.SUNKEN)
        separator.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=5, pady=5)
        
        return title_frame
    
    def create_scrollable_frame(self, parent):
        config_frame = tk.Frame(parent)
        config_frame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        
        config_canvas = tk.Canvas(config_frame)
        config_scrollbar = ttk.Scrollbar(config_frame, orient="vertical", command=config_canvas.yview)
        scrollable_frame = ttk.Frame(config_canvas)
        
        scrollable_frame.bind("<Configure>", 
                             lambda e: config_canvas.configure(scrollregion=config_canvas.bbox("all")))
        
        config_canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
        config_canvas.configure(yscrollcommand=config_scrollbar.set)
        
        config_canvas.pack(side="left", fill="both", expand=True)
        config_scrollbar.pack(side="right", fill="y")
        
        return config_canvas, config_scrollbar, scrollable_frame
    
    def create_config_entry_row(self, parent, name="", value=""):
        frame = ttk.Frame(parent)
        frame.pack(fill=tk.X, pady=5)
        
        ttk.Label(frame, text="名称:").pack(side=tk.LEFT, padx=5)
        name_entry = ttk.Entry(frame, width=40)
        name_entry.pack(side=tk.LEFT, padx=5)
        name_entry.insert(0, name)
        
        ttk.Label(frame, text="值:").pack(side=tk.LEFT, padx=5)
        value_entry = ttk.Entry(frame, width=25)
        value_entry.pack(side=tk.LEFT, padx=5)
        value_entry.insert(0, value)
        
        return frame, name_entry, value_entry
    
    def create_delete_button(self, parent, text="删除", fg="black", **kwargs):
        delete_btn = tk.Button(parent, text=text, fg=fg, **kwargs)
        delete_btn.pack(side=tk.LEFT, padx=5)
        return delete_btn
    
    def create_bottom_buttons(self, parent, buttons_config):
        button_frame = ttk.Frame(parent)
        button_frame.pack(fill=tk.X, padx=5, pady=10)
        
        for btn_config in buttons_config:
            position = btn_config.get('position', 'left')
            ttk.Button(button_frame, text=btn_config['text'], 
                      command=btn_config['command']).pack(side=position, padx=5)
        
        return button_frame
    
    def create_version_label(self, parent):
        version_frame = ttk.Frame(parent)
        version_frame.pack(fill=tk.X, padx=5, pady=5)
        
        version_label = ttk.Label(version_frame, text=self.VERSION, foreground="gray")
        version_label.pack(side=tk.RIGHT)
        
        return version_label
